/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigl.exporter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import sigl.model.Model;

/**
 *
 * @author uxmal
 */
public class GDF extends Exporter {

    public GDF(Model model) {
        super(model);
    }

    public void exportPackages(String filename, boolean noModules) {
        // parcours des modules et export
        //String define_nodes = "nodedef>name VARCHAR,label VARCHAR,visible BOOLEAN,width DOUBLE,height DOUBLE,";
        String define_nodes = "nodedef>name VARCHAR,label VARCHAR,pass INT,fail INT";
        String define_edges = "edgedef>node1 VARCHAR,node2 VARCHAR,directed BOOLEAN";
        int compteur = 0;
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
        } catch (java.io.IOException exc) {
            System.out.println("Erreur lors du dump.");
        }


        HashMap tmp = new HashMap();

        // itérateur  
        Set<Map.Entry<String, HashMap>> set = packages.entrySet();

        // pour chaque package
        for (Map.Entry<String, HashMap> entry : set) {
            String packageName = entry.getKey();
            // System.out.println("name: "+packageName);

            HashMap packageData = entry.getValue();
            // on récupère les modules
            if (packageData.containsKey("modules")) {
                ArrayList packageModules = (ArrayList) packageData.get("modules");

                Iterator modit = packageModules.iterator();
                while (modit.hasNext()) {
                    String moduleName = (String) modit.next();
                    if (!modules.containsKey(moduleName)) {
                        continue;
                    }

                    HashMap moduleData = (HashMap) modules.get(moduleName);
                    if (!moduleData.containsKey("dependencies")) {
                        continue;
                    }
                    // System.out.println("    ----> "+moduleName);

                    Iterator depit = ((ArrayList) moduleData.get("dependencies")).iterator();
                    while (depit.hasNext()) {
                        // on recupere le nom du package de la dependance
                        String dependency = (String) depit.next();
                        // System.out.print("         -  "+dependency+"");
                        HashMap dependencyData = (HashMap) modules.get(dependency);
                        if (dependencyData == null) {
                            continue;
                        }
                        String dependencyPackage = (String) dependencyData.get("package");

                        // note: si on travaille sur un mini graphe, la resolution du nom de package echouera
                        if (dependencyPackage == null && !noModules) {
                            dependencyPackage = dependency;
                        }

                        // System.out.println(" --> "+dependencyPackage);
                        // on l'ajoute comme arc : (package_courant) -----> (package_dont_on_depend)
                        if (!tmp.containsKey(packageName)) {
                            tmp.put(packageName, new ArrayList());
                        }

                        ((ArrayList) tmp.get(packageName)).add(dependencyPackage);
                    } // fin du parcours des dependances

                } // fin du parcours des modules

            } // fin du parcours des packages

        }


        int nodeCount = 0;
        // ECRITURE DU GDF
        writer.println(define_nodes);
        Set<Map.Entry<String, ArrayList>> edgeSet = tmp.entrySet();
        for (Map.Entry<String, ArrayList> entry : edgeSet) {
            String name = entry.getKey();
            String pass = "", fail = "";
// on est sur qu'il existe
            HashMap packageData = (HashMap) packages.get(name);
            if (packageData == null) {
                continue;
            }
            if (packageData.containsKey("pass")) {
                pass = "" + packageData.get("pass") + "";
                if (pass.equalsIgnoreCase("")) {
                    pass = "0";
                }
            }
            if (packageData.containsKey("fail")) {
                fail = "" + packageData.get("fail") + "";
                if (fail.equalsIgnoreCase("")) {
                    fail = "0";
                }
            }
            nodeCount++;
            writer.println("\"" + name + "\",\"" + name + "\"," + pass + "," + fail); // name,label

        }

        int edgeCount = 0;
        // ouf! on peut enfin parcourir la hashmap des arcs, nettoyées, sans doublons.
        writer.println(define_edges);
        // Set<Map.Entry<String, ArrayList>> edgeSet = edges.entrySet();
        for (Map.Entry<String, ArrayList> entry : edgeSet) {
            String name = entry.getKey();
            ArrayList deplist = entry.getValue();

            Iterator it = deplist.iterator();
            while (it.hasNext()) {
                edgeCount++;
                writer.println("\"" + name + "\",\"" + (String) it.next() + "\",true");
            }
        }
        writer.close();
        System.out.println("Exporté " + nodeCount + " noeuds / " + edgeCount + " arcs");

    }

    public void exportModules(String filename) {


        // parcours des modules et export
        //String define_nodes = "nodedef>name VARCHAR,label VARCHAR,visible BOOLEAN,width DOUBLE,height DOUBLE,";
        String define_nodes = "nodedef>name VARCHAR,label VARCHAR,pass INT, fail INT";
        String define_edges = "edgedef>node1 VARCHAR,node2 VARCHAR,directed BOOLEAN";
        int compteur = 0;
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
        } catch (java.io.IOException exc) {
            System.out.println("Erreur lors du dump.");
        }

        // itérateur  
        Set<Map.Entry<String, HashMap>> set = modules.entrySet();

        // NODES
        writer.println(define_nodes);
        for (Map.Entry entry : set) {
            String name = String.valueOf(entry.getKey()); // convertir n'importe quoi en string
            //String name = (String) ;
            // System.out.println(name);

            HashMap meta = (HashMap) entry.getValue();
            //int nodeDependencyCount = 10;
            String pass = "", fail = "";

            HashMap moduleData = (HashMap) modules.get(name);
            if (moduleData == null) {
                continue;
            }
            if (moduleData.containsKey("package")) {
                String packageName = (String) moduleData.get("package");

                if (packages.containsKey(packageName)) {
                    HashMap packageData = (HashMap) packages.get(packageName);

                    if (packageData.containsKey("pass")) {
                        pass = "" + packageData.get("pass") + "";
                        if (pass.equalsIgnoreCase("")) {
                            pass = "0";
                        }
                    }
                    if (packageData.containsKey("fail")) {
                        fail = "" + packageData.get("fail") + "";
                        if (fail.equalsIgnoreCase("")) {
                            fail = "0";
                        }
                    }
                }
            }

            writer.println("\"" + name + "\",\"" + name + "\"," + pass + "," + fail); // name,label

        }


        writer.println(define_edges);
        for (Map.Entry<String, HashMap> entry : set) {
            String name = String.valueOf(entry.getKey());
            //System.out.println("name: "+name);
            HashMap meta = entry.getValue();
            ArrayList dependencies = (ArrayList) meta.get("dependencies");
            if (dependencies == null) {
                continue;
            }
            Iterator it = dependencies.iterator();
            while (it.hasNext()) {
                compteur++;
                writer.println("\"" + name + "\",\"" + (String) it.next() + "\",true");
            }

        }
        System.out.println("Exporté " + modules.size() + " noeuds / " + compteur + " arcs");
        writer.close();

    }
}
